local sk__zhiti = fk.CreateSkill {

  name = "sk__zhiti",

  tags = {},

}



sk__zhiti:addEffect(fk.DamageCaused, {
  name = "sk__zhiti",
  anim_type = "control",
  can_trigger = function(self, event, target, player, data)
    if target and target == player and player:hasSkill(sk__zhiti.name) and data.to ~= player and not data.to.dead then
      local mark = player:getTableMark(sk__zhiti.name)
      mark[string.format("%.0f", data.to.id)] = mark[string.format("%.0f", data.to.id)] or {}
      event:setCostData(self, mark[string.format("%.0f", data.to.id)])
      return #event:getCostData(self) < 5
    end
  end,
  on_cost = function(self, event, target, player, data)
    local choices, all_choices = {}, {}
    for i = 1, 5, 1 do
      table.insert(all_choices, "sk__zhiti" .. i)
      if not table.contains(event:getCostData(self), i) then
        table.insert(choices, "sk__zhiti" .. i)
      end
    end
    table.insert(all_choices, "Cancel")
    table.insert(choices, "Cancel")
    if #choices > 1 then
      local choice = player.room:askForChoice(player, choices, sk__zhiti.name, "#sk__zhiti-invoke::" .. data.to.id, false,
        all_choices)
      if choice ~= "Cancel" then
        event:setCostData(self, { tonumber(choice[10]), event:getCostData(self) })
        return true
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local mark = player:getTableMark(sk__zhiti.name)
    local n = event:getCostData(self)[1]
    table.insert(event:getCostData(self)[2], n)
    mark[string.format("%.0f", data.to.id)] = event:getCostData(self)[2]
    room:setPlayerMark(player, sk__zhiti.name, mark)
    if n == 1 then
      room:loseHp(data.to, 1, sk__zhiti.name)
      room:changeMaxHp(data.to, -1)
      if not player.dead then
        room:changeMaxHp(player, 1)
        if not player.dead and player:isWounded() then
          room:recover({
            who = player,
            num = 1,
            recoverBy = player,
            skillName = sk__zhiti.name,
          })
        end
      end
    elseif n == 2 then
      room:setPlayerMark(data.to, MarkEnum.SkDrawNCards, data.to:getMark(MarkEnum.SkDrawNCards) - 1)
      room:addPlayerMark(player, MarkEnum.SkDrawNCards, 1)
    elseif n == 3 then
      local skill = ""
      local skills = table.filter(data.to:getSkillNameList(), function(s)
        return Fk.skills[s]:isPlayerSkill(data.to) and Fk.skills[s].visible and not player:hasSkill(s, true)
      end)
      if #skills > 0 then
        local result = room:askToCustomDialog(player, {
          skill_name = sk__zhiti.name,
          qml_path = "packages/utility/qml/ChooseSkillBox.qml",
          extra_data = {
            skills, 1, 1, "#sk__zhiti-choice::" .. data.to.id
          }
        })
        local skill = result
        if skill == "" then
          skill = table.random(skills)
        else
          skill = result[1]
        end
        room:handleAddLoseSkills(data.to, "-" .. skill, nil, true, false)
        if not player.dead then
          room:handleAddLoseSkills(player, skill, nil, true, false)
        end
      end
    elseif n == 4 then
      room:setPlayerMark(data.to, "@@sk__zhiti_ban", 1)
    elseif n == 5 then
      data.to:turnOver()
    end
  end,
})

sk__zhiti:addEffect("prohibit", {
  name = "#sk__zhiti_prohibit",
  prohibit_use = function(self, player, card)
    return player:getMark("@@sk__zhiti_ban") > 0 and card and card.type == Card.TypeEquip
  end,
})

return sk__zhiti
